<?
header('Content-type: text/javascript');
include("../../mysqlcon.php");
$reserv_id = null;

if(isset($_REQUEST['reserv_id'])){
	$reserv_id = $_REQUEST['reserv_id'];
}else{
	$reserv_id = 0;
}
mysql_select_db($dbname,$conn);
mysql_query('SET NAMES utf8');

$sql = "SELECT * FROM reservation WHERE reserv_gid = '$reserv_id' LIMIT 1";
$r = mysql_query($sql,$conn);
$rs = mysql_fetch_array($r);

?>
$(document).ready(function() {
	var nowDate = new Date();
	nowDate.addDays(-1);
	var now2Date = new Date();
	now2Date.addDays(0);
	now2Date.setHours(0,0,0,0);
	var beginDate = new Date();
	var endDate = new Date();
	var beginDateFormated;
	var endDateFormated;
	var fromDateFormated;
	var toDateFormated;
	var inputDate;
	var perNight;
	$('#fromDate').val('<?echo $rs['reserv_begin'];?>');
	$('#toDate').val('<?echo $rs['reserv_end'];?>');
	if ( '<?echo $rs['reserv_begin'];?>' != '' && '<?echo $rs['reserv_end'];?>' != '' ) {
		fromDateFormated = $('#fromDate').val();
		toDateFormated = $('#toDate').val();
		inputDate = $('#fromDate').val()+","+$('#toDate').val();
		$('#inputDate').val(inputDate);
		$.ajax({
	      url: './validate/validateListRoom.php',
	      type: 'GET',
	      data: {date: $("#inputDate").val(), reserv_id: '<?echo $reserv_id; ?>'},
	      error: function(x){
				alert("error");
			},
	      success: function(data) {
     	 	 var toDate = $("#toDate").val();
			 var fromDate = $("#fromDate").val();
			      	 
			 toDate = toDate.replace(/-/gi, "/");
			 fromDate = fromDate.replace(/-/gi, "/");

			 perNight = (Date.parse(toDate) - Date.parse(fromDate))/86400000;
	      	 $("#perNight").html("<font style='color:red;'>"+perNight+" คืน </font>");
	         $(".night").show();
	         $("#roomlist").html(data);
	         
	      }
	   });
	}
	$('#fromDate').DatePicker({
		format:'Y-m-d',
		date: '<?echo $rs['reserv_begin'];?>',
		current: nowDate,
		starts: 1,
		position: 'right',
		onRender: function(date) {
			return {
				disabled: (date.valueOf() < nowDate.valueOf()),
				className: date.valueOf() == now2Date.valueOf() ? 'datepickerSpecial' : false
			}
		},
		onBeforeShow: function(){
			if ($('#fromDate').val() != '') {
				$('#fromDate').DatePickerSetDate($('#fromDate').val(), true);
			}
		},
		onChange: function(formated, dates){
			beginDateFormated = dates;
			fromDateFormated = formated;
			if(endDateFormated != 'undefined'){
				if((beginDateFormated >= endDateFormated)){
					alert("test");
					return false;
				}else{
					$('#fromDate').val(formated);
				}
			}else{
				$('#fromDate').val(formated);
			}
			beginDate = dates == '' ? '<?echo $rs['reserv_end'];?>' : dates;
			$('#fromDate').DatePickerHide();
			
			if(typeof (toDateFormated) != 'undefined'){
				inputDate = $('#fromDate').val()+","+toDateFormated;
				$('#inputDate').val(inputDate);
				if(mode=="2"){
					$.ajax({
				      url: './validate/validateListRoom.php',
				      type: 'GET',
				      data: {date: $("#inputDate").val(), reserv_id: '<?echo $reserv_id; ?>'},
				      error: function(x){
							alert("error");
						},
				      success: function(data) {
				      	 var toDate = $("#toDate").val();
				      	 var fromDate = $("#fromDate").val();
				      	 
				      	 toDate = toDate.replace(/-/gi, "/");
				      	 fromDate = fromDate.replace(/-/gi, "/");

				      	 perNight = (Date.parse(toDate) - Date.parse(fromDate))/86400000;
				      	 $("#perNight").html("<font style='color:red;'>"+perNight+" คืน </font>");
				      	 $(".night").show();
				         $("#roomlist").html(data);
				      }
				   });
				}
			}else{
				$("#roomlist").html("<div style='float: left; margin-right: 10px; margin-left: 50px;'>no item found</div>");
			}
		}
	});
	
	var nowDate2 = new Date();
	nowDate2.addDays(-1);
	var now2Date2 = new Date();
	now2Date2.addDays(0);
	now2Date2.setHours(0,0,0,0);
	$('#toDate').DatePicker({
		format:'Y-m-d',
		date: beginDate,
		current: beginDate,
		starts: 1,
		position: 'right',
		onRender: function(date) {
			return {
				disabled: (date.valueOf() < Date.parse(beginDate)+3600),
				className: date.valueOf() == now2Date2.valueOf() ? 'datepickerSpecial' : false
			}
		},
		onBeforeShow: function(){
			if ($('#toDate').val() != '') {
				$('#toDate').DatePickerSetDate($('#toDate').val(), true);
			}
		},
		onChange: function(formated, dates){
			toDateFormated = formated;
			$('#toDate').val(formated);
			endDateFormated = dates;
			endDate = dates;
			$('#toDate').DatePickerHide();
			if(typeof (fromDateFormated) != 'undefined'){
				inputDate = fromDateFormated+","+$("#toDate").val();
				$('#inputDate').val(inputDate);
				if(mode=="2"){
					$.ajax({
				      url: './validate/validateListRoom.php',
				      type: 'GET',
				      data: {date: $("#inputDate").val(), reserv_id: '<?echo $reserv_id;?>'},
				      error: function(x){
							alert("error");
						},
				      success: function(data) {
				      	 var toDate = $("#toDate").val();
				      	 var fromDate = $("#fromDate").val();
				      	 
				      	 toDate = toDate.replace(/-/gi, "/");
				      	 fromDate = fromDate.replace(/-/gi, "/");

				      	 perNight = (Date.parse(toDate) - Date.parse(fromDate))/86400000;

				      	 $("#perNight").html("<font style='color:red;'>"+perNight+" คืน </font>");
				      	 $(".night").show();
				         $("#roomlist").html(data);
				      }
				   });
				}
			}else{
				$("#roomlist").html("<div style='float: left; margin-right: 10px; margin-left: 50px;'>no item found</div>");
			}
		}
	});
});
